package com.augmentum.oes.model;

import java.util.List;

import com.augmentum.oes.util.PropertyUtil;

public class Pagination<T> {
    private static final String PAGE_SIZE = "pagination.size";

    private int totalCount;

    private int pageSize;
    private int pageCount;
    private int currentPage;
    private int offset;
    private int order;
    private String fuzzyWord;
    private String fuzzyTrans;
    private List<T> itemList;

    public Pagination() {
        order = 1;
    }

    public Pagination(int totalCount, int pageSize, int currentPage, int order, String fuzzyWord) {
        super();
        this.totalCount = totalCount;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.order = order;
        this.fuzzyWord = fuzzyWord;
    }

    public int getPageCount() {
        if (totalCount < 1) {
            return 1;
        } else {
            return (totalCount - 1) / getPageSize() + 1;
        }
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getPageSize() {
        if (pageSize == 0) {
            String page = PropertyUtil.getJDBCInfo(PAGE_SIZE);
            pageSize = Integer.parseInt(page);
        }
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getCurrentPage() {
        if (totalCount < 1 || currentPage < 1) {
            return 1;
        }
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getOffset() {
        return (getCurrentPage() - 1) * getPageSize();
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public String getFuzzyWord() {
        if (fuzzyWord == null) {
            return "";
        } else {
            return fuzzyWord;
        }
    }

    public void setFuzzyWord(String fuzzyWord) {
        this.fuzzyWord = fuzzyWord;
    }
    

    public String getFuzzyTrans() {
        return fuzzyTrans;
    }

    public void setFuzzyTrans(String fuzzyTrans) {
        this.fuzzyTrans = fuzzyTrans;
    }
    

    public List<T> getItemList() {
        return itemList;
    }

    public void setItemList(List<T> itemList) {
        this.itemList = itemList;
    }

    @Override
    public String toString() {
        return "Pagination [totalCount=" + totalCount + ", pageSize=" + pageSize + ", pageCount=" + pageCount
                + ", currentPage=" + currentPage + ", offset=" + offset + ", order=" + order + ", fuzzyWord="
                + fuzzyWord + "]";
    }

}
